/**
 * 快捷加入购物车 ---- 商品推荐右下角快捷加入购物车
 */
define(function(require,exports,module) { 
	var myPub = require('./public');
	
	//添加购物车动画
	var oMoveIcon = $('.js_moveIcon');          			// 加入购物车动态标记
	var oFooter = $('.js_footer');      					// 底部
	var oShopCarTip = $('.js_userNum');						// 数量记录
    var oGoodsAddQuick = $('.js_goodsAddQuick');
    var oSideBox = $('.js_sideBox');						// 侧边栏
    var oLoadingPage = $('.js_loadingPage');				// 加载框

    var oShopCar_tip = $('.js_shopCar_tip');                // 收藏加入购物车提示

    var AddBtn, clearTime, time1, time2, addOneTime;
    var dbTapOff = true;
    oGoodsAddQuick.hammer().off('tap');
	oGoodsAddQuick.hammer().on('tap',function(e) {
		if(dbTapOff) { //阻止多次点击 true 可点击  
		    var _tag = e.gesture.target;
            var $target;
            _tag.nodeName.toLowerCase() == 'p' ? $target = $(_tag) : $target = $(_tag).parents('p') 
		    var _X = e.gesture.center.x;
		    var _Y = e.gesture.center.y;
		    var win_Width = $(window).width();
           	var win_Height = $(window).height();
		    var oFooterH = oFooter.height();

		    var oJSAddCar = $target.parents('li').find('.js_addCar');
		     
		    var pid = oJSAddCar.attr('pId');
            var regionId = oJSAddCar.attr('regionId') == undefined ? "" : oJSAddCar.attr('regionId');


            var $requestid = oJSAddCar.attr('requestid');
            var requestid = '';
            var scenetype = '';
            if( $requestid && $requestid != 'undefined' && $requestid != 'null' ) {
                requestid = $requestid
            }
            var goodschannel = oJSAddCar.attr('goodschannel') == undefined ? "" : oJSAddCar.attr('goodschannel');

            if( goodschannel == 'datagrand' || goodschannel == 'not_datagrand' ){
                scenetype = 'cate';
            }

		    var quantity = 1;

		    if($target.parents('li').find('.js_sumNum').length != 0){
		    	quantity = $target.parents('li').find('.js_sumNum').val();
		    }
		    if($target.length > 0 && $target[0].className == 'addCar js_addCar') {
		    	var imgSrc = $target.parents('li').find('figure img').attr('src');
                var sLeft = '';
                var sTop = '';
                oMoveIcon.find('img').attr('src', imgSrc);
		    	dbTapOff = false; 
		    	//var aLi = $(this).find('.js_addCar');
		    	//判断是商家页面 改变动画 left和top
                if(window.location.pathname.search('h/store/normalStore') > 0) {
	    			sLeft = oSideBox.position().left + oMoveIcon.width()/2;
                	sTop = oSideBox.position().top + oMoveIcon.width()*1.5;
	    		}

                //判断是购物车列表成弹框
	    		else if(window.location.pathname.search('cartItem/showCart') > 0) {
                    oLoadingPage.show();
				}
	    		else {
	    			sLeft = win_Width * 0.63 - oMoveIcon.width()/2;
                	sTop = win_Height - oFooterH - oMoveIcon.height() - oMoveIcon.height()/10;
	    		}

                G_JSON_POST('/shop/cartItem/addCart', {
                    "pId": pid,
                    "quantity": quantity,
                    "regionId":regionId,
                    "goodschannel": goodschannel
                }, function (data) {
                    if (data.status == "success") {
                        var totalQuantity = data.totalQuantity;
                        if($('.js_rightActive').length>0){
                            $target.append('<i class="addCarAnimate js_addCarAnimate">+1</i>');
                            clearTimeout(addOneTime);
                            addOneTime = setTimeout(function () {
                                $('.js_addCarAnimate').remove();
                                dbTapOff = true;
                            },1200)
                        }else {
                            myPub.AddCarAnimate(_X, _Y, oMoveIcon, sLeft, sTop, oFooterH, function () {
                                // 修改购物车数量
                                oShopCarTip.show().html(totalQuantity).addClass('mybounceIn')
                                // oShopCarTip.show().addClass('mybounceIn');
                                clearTimeout(clearTime);
                                clearTime = setTimeout(function () {
                                    oShopCarTip.removeClass('mybounceIn');
                                    dbTapOff = true;
                                }, 1200);
                            });
                        }
                        if (window.location.pathname.search('cartItem/showCart') > 0 || window.location.pathname.search('myInfo/toMyInfo')) {
                            var tipText = '<span class="icon-check-cir icon_myLayer_red"></span><span class="icon_myLayer_normal">加入购物车成功！</span>';
                            var tipStyle = 'background-color: rgba(0,0,0,.6); color: #FFFFFF; min-width: 25%;';

                            if(oShopCarTip.length > 0) {
                                myPub.AddCarAnimate(_X, _Y, oMoveIcon, sLeft, sTop, oFooterH, function () {
                                    // 修改购物车数量
                                    oShopCarTip.show().html(totalQuantity).addClass('mybounceIn');
                                    // oShopCarTip.show().addClass('mybounceIn');
                                    clearTimeout(clearTime);
                                    clearTime = setTimeout(function () {
                                        oShopCarTip.removeClass('mybounceIn');
                                        dbTapOff = true;
                                    }, 1200);
                                });
                            }
                            else  {
                                myPub.TipLayer(tipText, tipStyle, function () {
                                    oLoadingPage.hide();
                                });
                            }

                        }

                        // 收藏加入购物车提示
                        if(oShopCar_tip.length>0) {
                            oShopCar_tip.fadeIn();
                        }

                        // 添加用户行为
                        var setActionData = {};
                        setActionData.pid = pid * 1;
                        setActionData.num = 1;
                        setActionData.requestid = requestid;
                        setActionData.scenetype = scenetype;

                        // setActionData.goodschannel = goodschannel;
                        // console.log('setActionData', setActionData)
                        userAction($target, 'cart', setActionData);


                    } else {
                        myPub.TipLayer(data.message);
                        oLoadingPage.hide();
                        clearTimeout(time1);
                        time1 = setTimeout(function () {
                            dbTapOff = true;
                        }, 1200)

                    }
                    myPub.HamstopPropaga();

                }, function () {
                }, function () {
                    //完成
                    clearTimeout(time2);
                    time2 = setTimeout(function () {
                        oShopCar_tip.fadeOut();
                        dbTapOff = true;
                    }, 1200);
                });
		    	
		       	myPub.HamstopPropaga();
		    	e.stopPropagation();
		    }
		     
		    
	    }
	});


    // 添加用户行为
    function userAction(obj, mode, keyword) {
        ddjhoc.setData(obj, mode, 'add', function () {
            console.log('params.'+mode, ddjhoc.params);
            ddjhoc.sendData( ddjhoc.params );
        }, keyword);
    }
});

